/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.netease.financial.base.photo;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

import com.netease.financial.R;
import com.netease.financial.common.util.PhoneCapabilityTester;

import java.util.ArrayList;

/**
 * Shows a popup asking the user what to do for a photo. The result is passed back to the Listener
 */
public class PhotoActionPopup {
    public static final String TAG = PhotoActionPopup.class.getSimpleName();

    public static Dialog createDialog(Context context, final Listener listener) {
        final ListAdapter adapter = getListAdapter(context);

        final AlertDialog dialog = new AlertDialog.Builder(context)
                .setAdapter(adapter, null)
                .create();

        dialog.setCanceledOnTouchOutside(true);

        dialog.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                pickItem(listener, adapter, position);
                dialog.dismiss();
            }
        });

        return dialog;
    }

    private static void pickItem(Listener listener, ListAdapter adapter, int position) {
        final ChoiceListItem choice = (ChoiceListItem) adapter.getItem(position);
        switch (choice.getId()) {
            case ChoiceListItem.ID_TAKE_PHOTO:
                listener.onTakePhotoChosen();
                break;
            case ChoiceListItem.ID_PICK_PHOTO:
                listener.onPickFromGalleryChosen();
                break;
            default:
                break;
        }
    }

    private static ListAdapter getListAdapter(Context context) {
        final ArrayList<ChoiceListItem> choices = new ArrayList<>(2);

        // Take photo or pick one from the gallery.
        if (PhoneCapabilityTester.isCameraIntentRegistered(context)) {
            choices.add(new ChoiceListItem(ChoiceListItem.ID_TAKE_PHOTO, context.getString(R.string.take_photo)));
        }
        choices.add(new ChoiceListItem(ChoiceListItem.ID_PICK_PHOTO, context.getString(R.string.pick_photo)));

        return new ArrayAdapter<>(context, R.layout.select_dialog_item, choices);
    }

    private static final class ChoiceListItem {
        private final int mId;
        private final String mCaption;

        public static final int ID_TAKE_PHOTO = 0;
        public static final int ID_PICK_PHOTO = 1;

        public ChoiceListItem(int id, String caption) {
            mId = id;
            mCaption = caption;
        }

        @Override
        public String toString() {
            return mCaption;
        }

        public int getId() {
            return mId;
        }
    }

    public interface Listener {
        void onTakePhotoChosen();

        void onPickFromGalleryChosen();
    }
}
